﻿using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Linq;

using VIT.Web.UI;
using VIT.Wedding;
using VIT.Library.Web;
using VIT.DataHelper.DataAccess;

namespace VIT.TestWeb.Product1
{
    public partial class Detail : VIT.Web.UI.VITComponent
    {
        public struct ColorOrStyle
        {
            public int ID;
            public string Type;
            public string Name;
        } 
        public ColorOrStyle obj;
        private dynamic objCS;

        #region properties

        private string sId
        {
            get { return Request["sId"]; }
        }
        private string ColorId
        {
            get { return string.IsNullOrEmpty(Page.Request.Params.Get("sColorsId")) ? "0" : Page.Request.Params.Get("sColorsId"); }
        }
        private string StyleId
        {
            get { return string.IsNullOrEmpty(Page.Request.Params.Get("sStylesId")) ? "0" : Page.Request.Params.Get("sStylesId"); }
        }
        #endregion

        private void LoadSkinBannerProduct(string catId)
        {
            if (!string.IsNullOrEmpty(catId))
            {
                var pars = new Dictionary<string, string>();
                pars["Id"] = catId;
                LoadModule(SkinBannerProduct, "CategoryDetail", pars);
            }
        }

        private void LoadSkinBanner(string Position)
        {
            var pars = new Dictionary<string, string>();
            pars["Position"] = Position;
            pars["Width"] = "732";
            pars["Height"] = "301";
            LoadModule(SkinBanner, "SlideshowNumber", pars);
        }

        private void LoadSkinContent(string Id)
        {
            var pars = new Dictionary<string, string>();
            pars["sId"] = Id;
            LoadModule(SkinContent, "ContentDetail", pars);
        }

        private void LoadSkinCenter(string Id, string catID, string catName)
        {
            if (!string.IsNullOrEmpty(catID))
            {
                var pars = new Dictionary<string, string>();
                pars["Top"] = "0";
                pars["PageSize"] = "8";
                pars["Category"] = catID;
                pars["InChildCat"] = "true";
                pars["HasFavourite"] = "false";
                pars["ProductNotIn"] = Id;
                //pars["Color"] = ColorId;
                //pars["Style"] = StyleId;
                LoadModule(SkinCenter, "Products", pars, catName);
            }
        }

        protected int ColorIdCurrent
        {
            get;
            private set;
        }
        
        protected string ColorNameCurrent
        {
            get;
            private set;
        }

        private void loadChoose(global::VIT.DataHelper.DataAccess.Product product)
        {
            string lang = Session["language"].ToString();
            if (ColorId == "0" && StyleId == "0")
            {
                obj.ID = 0;
                LoadSkinBannerProduct(product.CategoryId.ToString());
                var catlan = VITData.GetObject<global::VIT.DataHelper.DataAccess.Category_Language>(o =>o.CategoryId == product.CategoryId && o.LanguageId == lang);
                LoadSkinCenter(product.Id.ToString(), product.CategoryId.ToString(),catlan.Title + " cùng loại");
            }
            else
            {
                if (ColorId != "0")
                {
                    if (int.TryParse(ColorId, out obj.ID))
                    {
                        objCS = VITData.GetObject<Product_Color>(o => o.Id == obj.ID);
                        obj.Type = "màu";
                        obj.Name = objCS.Name;
                        LoadSkinBannerProduct("54");

                        var stylesRec = VITData.GetQuery<Product_Color_Style>(c => c.ColorId == Convert.ToInt32(ColorId)).ToArray();
                        var styles = VITData.GetQuery<Product_Style>().ToArray();
                        var productRec = (from style in styles
                                               join styleRec in stylesRec on style.Id equals styleRec.StyleId
                                               select style).ToArray();
                        rptChoose.DataSource = productRec;
                        rptChoose.DataBind();
                    }
                }
                else // StyleId != "0"
                {
                    if (int.TryParse(StyleId, out obj.ID))
                    {
                        objCS = VITData.GetObject<Product_Style>(o => o.Id == obj.ID);
                        obj.Type = "style";
                        obj.Name = objCS.Name;
                        LoadSkinBannerProduct("55");

                        var colorsRec = VITData.GetQuery<Product_Color_Style>(c => c.ColorId == Convert.ToInt32(StyleId)).ToArray();
                        var colors = VITData.GetQuery<Product_Color>().ToArray();
                        var productRec = (from color in colors
                                          join colorRec in colorsRec on color.Id equals colorRec.StyleId
                                          select color).ToArray();
                        rptChoose.DataSource = productRec;
                        rptChoose.DataBind();
                    }                   
                }

                var cats = VITData.GetQuery<global::VIT.DataHelper.DataAccess.Category>().ToArray();
                var catsLang = VITData.GetQuery<Category_Language>(c => c.LanguageId == lang).ToArray();
                var productsSame = (from lstcatlang in catsLang
                                    join lstcat in cats on lstcatlang.CategoryId equals lstcat.Id
                                    where lstcat.ParentId == 2
                                    select lstcatlang).Distinct().ToArray();

                rptProductSame.DataSource = productsSame;
                rptProductSame.DataBind();
            }
        }

        protected new void Page_PreInit(Object sender, System.EventArgs e)
        {
            base.Page_PreInit(sender, e);
            int Id = 0;
            if (!string.IsNullOrEmpty(sId)) Id = Convert.ToInt32(sId);
            var product = VITData.GetObject<global::VIT.DataHelper.DataAccess.Product>(c => c.Id == Id);
            //LoadSkinBanner("BannerProducts");
            if (product != null)
            {
                loadChoose(product);
                LoadSkinContent(sId);
            }
        }

        public string phone = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            var company = VITData.GetObject<CompanyDetail>(c => c.Id > 0);
            if (company != null) phone = company.Phone;
        }
    }
}